package com.demo.application.dao;

import com.demo.application.entity.StockEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * Author: kosam
 */
public interface StockRepository extends JpaRepository<StockEntity, Integer> {

    @Query(value = "SELECT id as id ,commodity_id as  commodityId, stock as stock from stock where commodity_id = ?1", nativeQuery = true)
    StockEntity selectStock(@Param("commodityId") Long commodityId);

    @Modifying @Query(value = "update stock set stock=stock-1 where commodity_id =?1", nativeQuery = true)
    int updateStock(@Param("commodityId") Long commodityId);

    StockEntity findByCommodityId(Long commodityId);
}
